package kechengsheji.employee;

import gongju.Communtil;
import gongju.JDBC;
import gongju.User;
import java.util.ArrayList;
import java.util.List;

//普通员工
public class UserDAO {
    //属性
    public static final Object[] columnNames = {"ID", "员工名", "部门", "密码"};
//    //存储数据，用于模拟数据库中表
//    public static final List<Object[]> data = new ArrayList<>();
//    private static int maxID = 3;
//    //初始数据
//    static {
//        data.add(new Object[]{0,"甲","维护部","1234"});
//        data.add(new Object[]{1,"已","修理部","1234"});
//        data.add(new Object[]{2,"丙","人事部","1234"});
//        data.add(new Object[]{3,"丁","开发部","1234"});
//    }
//
//    public static User getUser(String username){
//        for(Object[] d : data){
//            if(d[1].equals(username)){
//                User user = new User();
//                user.setId((int)d[0]);
//                user.setUsername((String)d[1]);
//                user.setPassword((String)d[3]);
//                user.setType(3);
//                return user;
//            }
//        }
//        return null;
//    }

    public static User getUser(String name) {
        String sql = "select id,name,dmt,password from user where name=?";
        List param = new ArrayList();
        param.add(name);
        Object[] obj = JDBC.queryObject(sql, param, 4);
        if (obj == null) {
            return null;
        }
        User user = new User();
        user.setId((int) obj[0]);
        user.setUsername((String) obj[1]);
        user.setPassword((String) obj[3]);
        user.setType(2);
        return user;
    }

    public static Object[][] searvhMulti(int[] cols, String[] texts) {
      String sql = "select id,name,dmt,password from user where dmt=?";
      List param = new ArrayList();
      if(cols!= null && cols.length > 0) {
          for(int i = 0; i < cols.length; i++) {
              if(Communtil.isEmpty(texts[i]))
                  continue;
              switch (cols[i]) {
                  case 1:
                      sql += " and name like concat('%',?,'%')";
                      break;
                      case 2:
                          sql += " and name like concat('%',?,'%')";
                          break;
              }
              param.add(texts[i]);
          }
      }
      return Communtil.toArray(JDBC.queryList(sql,param,4));
    }


    public static void add(Object[] obj) {
        String sql = "insert into user (name,dmt,password,type) values(?,?,?,?)";
        List param = new ArrayList();
        for (int i = 0; i < obj.length; i++) {
            param.add(obj[i]);
        }
        JDBC.executeUpdate(sql, param);
    }

    public static Object[] findById(Object id) {
       String sql = "select * from user where id=?";
       List param = new ArrayList();
       param.add(id);
      return JDBC.queryObject(sql, param, 4);
    }

    public static void updata(Object id, Object[] obj) {
      String sql = "update user set name=?,dmt=?,password=?,type=? where id=?";
      List param = new ArrayList();
      param.add(obj[2]);
      param.add(obj[3]);
      param.add(obj[4]);
      param.add(id);
      JDBC.executeUpdate(sql, param);
    }

    public static void remove(Object id) {
        String sql = "delete from user where id=?";
        List param = new ArrayList();
        param.add(id);
        JDBC.executeUpdate(sql, param);
    }

}
